`
https://leetcode.cn/problems/find-bottom-left-tree-value/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var findBottomLeftValue = function (root) {
  let res = root.val
  let maxDepth = 0

  const traverse = (root, depth) => {
    if (root === null) return

    // 到最大深度时第一次遇到的节点就是左下角的节点
    if (depth > maxDepth) {
      // 更新结果
      res = root.val
      // 更新深度
      maxDepth = depth
    }

    traverse(root.left, depth + 1)
    traverse(root.right, depth + 1)
  }

  traverse(root.left, 1)
  traverse(root.right, 1)

  return res
};